package io.metaxk.module.mes.controller.admin.pro;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import io.metaxk.module.mes.controller.admin.pro.vo.ProcessExcelVo;
import io.metaxk.module.mes.controller.admin.pro.vo.ProcessQueryVo;
import io.metaxk.module.mes.dal.dataobject.pro.Process;
import io.metaxk.module.mes.service.pro.ProcessService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import javax.servlet.http.*;
import java.net.URLEncoder;
import java.util.*;
import java.io.IOException;
import io.metaxk.framework.common.pojo.PageResult;
import io.metaxk.framework.common.pojo.CommonResult;
import static io.metaxk.framework.common.exception.util.ServiceExceptionUtil.exception;
import static io.metaxk.framework.common.pojo.CommonResult.success;
import static io.metaxk.module.mes.enums.ErrorCodeConstants.*;




/**
 * @author 万界星空
 * @time 2023/6/20 15:33
 */
@Tag(name = "管理后台 - 生产工序")
@RestController
@RequestMapping("/mes/pro/process")
public class ProcessController {



    @Resource
    private ProcessService processService;



    @GetMapping("/availableList")
    @Operation(summary = "查询可用工序")
    public CommonResult<List<Process>> findAvailableList() {
        List<Process> list = processService.findAvailableList();
        return success(list);
    }




    @GetMapping("/list")
    @Operation(summary = "生产工序条件分页")
    @PreAuthorize("@ss.hasPermission('pro:process:list')")
    public CommonResult<PageResult<Process>> processPage(ProcessQueryVo pageVO) {
        PageResult<Process> pageResult = processService.getProcessPage(pageVO);
        return success(pageResult);
    }


    @PostMapping("/save")
    @Operation(summary = "新增生产工序")
    @PreAuthorize("@ss.hasPermission('pro:process:save')")
    public CommonResult<Integer>  save(@RequestBody Process process) {
        if(processService.findProcessByCode(process.getProcessCode()) != null){
            throw  exception(PROCESS_CODE_EXIST);
        }
        if(processService.findProcessByName(process.getProcessName()) != null){
            throw  exception(PROCESS_NAME_EXIST);
        }
        return success(processService.insertProcess(process)).setMsg("新增成功");
    }


    @PutMapping("/update")
    @Operation(summary = "修改生产工序")
    @PreAuthorize("@ss.hasPermission('pro:process:update')")
    public CommonResult<Integer> update( @RequestBody Process process){
        return success(processService.updateProcess(process)).setMsg("修改成功");
    }



    @DeleteMapping("/batch")
    @Operation(summary = "删除生产工序")
    @PreAuthorize("@ss.hasPermission('pro:process:delete')")
    public  CommonResult<Integer> remove(@RequestBody List<Long> ids){
        return success( processService.removesProcess(ids)).setMsg("删除成功");
    }





//    @GetMapping("/listAll")
//    @Operation(summary = "查询所有可用工序")
//    public CommonResult<List<Process>> listAll(){
//        Process process = new Process();
//        process.setEnableFlag("Y");
//        List<Process> list =processService.selectProProcessList(process);
//        return success(list);
//    }



    @GetMapping("/get/{id}")
    @Operation(summary = "生产工序详情")
    @PreAuthorize("@ss.hasPermission('pro:process:get')")
    public CommonResult<Process>  getProcess(@PathVariable Long id) {
        return success(processService.findProcessById(id));
    }





    @GetMapping("/export")
    @Operation(summary = "生产工序导出")
    @PreAuthorize("@ss.hasPermission('pro:process:export')")
    public void export(HttpServletResponse response){
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("工序管理", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
            SimpleColumnWidthStyleStrategy styleStrategy = new SimpleColumnWidthStyleStrategy(20);
            EasyExcel.write(response.getOutputStream(), ProcessExcelVo.class).registerWriteHandler(styleStrategy).sheet("工序管理").doWrite(processService.listData());
        } catch (IOException e) {
            throw exception(EXPORT_DATA_ERROR);
        }
    }

}
